package data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class Database {
	public static Vector<Card> cardlist;
	public static void init() {
		try {
			Class.forName("org.sqlite.JDBC");
			System.out.println("Loading JDBC driver -- OK!"); 
			cardlist = Database.readCard();
			System.out.println("Loading card reference -- OK!"); 
		} catch(ClassNotFoundException e) {
			System.out.println("Loading JDBC driver -- Error!\nSystem Exits");
			System.exit(1);
		}
	}
	public static Connection load(String database) {
		Connection connex = null;
		try {
			connex = DriverManager.getConnection("jdbc:sqlite:./Data/"+database);
			connex.setAutoCommit(false);
			System.out.println("Opening database -- OK!");
		} catch(Exception e) {
			System.out.println("Opening database -- Error!");
			System.exit(1);
		}
		return connex;
	}
	public static Vector<Card> readCard() {
		Vector<Card> cardlist = new Vector<Card>();
		Connection connex = load("static.db");

		Card card = null;
		Statement stmt = null;
		ResultSet rst = null;
		try {
			stmt = connex.createStatement();
			rst = stmt.executeQuery( "SELECT * FROM CARDINFO");
			while (rst.next()){	// If info exists
				card = new Card(rst.getString("NAME"), rst.getInt("RANK"), rst.getInt("SKILL"));
				card.minhp = rst.getInt("MINHP");
				card.minat = rst.getInt("MINAT");
				card.maxhp = rst.getInt("MAXHP");
				card.maxat = rst.getInt("MAXAT");
				card.level = 1;
				card.limit = rst.getInt("LIMIT");
				cardlist.add(card);
			}
			rst.close();
			stmt.close();
			connex.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cardlist;
	}
}